home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4089 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: cscsun3.larc.nasa.gov!hook
  2. From: hook@cscsun3.larc.nasa.gov (Ed Hook)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Float calculations
  5. Date: 1 Feb 1996 22:02:56 GMT
  6. Organization: CSC/NASA Langley Research Center
  7. Distribution: world
  8. Message-ID: <4erdag$qq5@reznor.larc.nasa.gov>
  9. References: <4eqssf$d9q@camelot.ccs.neu.edu> <DM458u.F1y@microunity.com>
  10. Reply-To: hook@cscsun3.larc.nasa.gov
  11. NNTP-Posting-Host: cscsun3.larc.nasa.gov
  12.  
  13. In article <DM458u.F1y@microunity.com>, toms@MicroUnity.com (Tom Sanders) writes:
  14. |> In article <4eqssf$d9q@camelot.ccs.neu.edu>, jason@ccs.neu.edu (Jason Leatherman) writes:
  15. |> |> 
  16. |> |> Hi,
  17. |> |> 
  18. |> |>   Check out the results I get when running this simple program on a
  19. |> |> Sparc/UNIX system, compiled with gcc:
  20. |> |> 
  21. |> |> #include <stdio.h>
  22. |> |> 
  23. |> |> void main()
  24. |> |> {
  25. |> |>   float a, b;
  26. |> |> 
  27. |> |>   printf("%0.10f  %0.10f  %0.10f\n", 99974.0, 50.0, 99974.0/50.0);
  28. |> |> 
  29. |> |>   a = 99974.0;
  30. |> |>   b = 50.0;
  31. |> |>   printf("%0.10f  %0.10f  %0.10f\n", a, b, a/b);
  32. |> |> }
  33. |> |> 
  34. |> |> The output is:
  35. |> |> 99974.0000000000  50.0000000000  1999.4800000000
  36. |> |> 99974.0000000000  50.0000000000  1999.4799804688
  37. |> |> 
  38. |> |>   Why do the divisions produce different results?  This is probably some
  39. |> |> simple thing that I've forgotten, but I haven't figured it out yet.  Does
  40. |> |> anyone know?  Note that compiling with the -ffloat-store flag didn't make
  41. |> |> a difference.
  42. |> |>   Thanks for any help,
  43. |> |> 
  44. |> |> Jason
  45. |> 
  46. |> For what it's worth, cc on both HP and Sun workstations do not behave
  47. |> this way, but gcc on both machines do.  
  48. |> 
  49. |> Tom Sanders
  50.  
  51.   Surely the explanation is as simple as noting that unadorned floating-point
  52.   constants are 'double's -- therefore, the computations to determine the
  53.   arguments for the first 'printf()' call are done in double precision. OTOH,
  54.   the calculation of 'a/b' in the second call *can* be done in single
  55.   precision and promoted to 'double'. The second poster's observation is
  56.   then evidence that the 'cc' implementations 'double' everything, while
  57.   'gcc' takes a more literal approach ...
  58.  
  59. -- 
  60.  Ed Hook                              |       Coppula eam, se non posit
  61.  Computer Sciences Corporation        |         acceptera jocularum.
  62.  NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>
  63.  Internet: hook@cscsun3.larc.nasa.gov |        ... Get a _clue_ !!! ...
  64.